package com.vero.appointment.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import com.vero.common.core.utils.DateUtils;
import com.vero.common.core.utils.StringUtils;
import com.vero.common.security.utils.SecurityUtils;
import com.vero.system.api.RemoteInventoryService;
import com.vero.system.api.RemoteNotificationService;
import com.vero.system.api.domain.Vaccine;
import com.vero.system.api.model.EmailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.vero.appointment.mapper.AppointmentVaccinationMapper;
import com.vero.system.api.domain.AppointmentVaccination;
import com.vero.appointment.service.IAppointmentVaccinationService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;

/**
 * 预约接种记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-06
 */
@Service
public class AppointmentVaccinationServiceImpl implements IAppointmentVaccinationService {
    @Autowired
    private AppointmentVaccinationMapper appointmentVaccinationMapper;

    /**
     * 查询预约接种记录
     *
     * @param recordId 预约接种记录主键
     * @return 预约接种记录
     */
    @Override
    public AppointmentVaccination selectAppointmentVaccinationByRecordId(Long recordId) {
        return appointmentVaccinationMapper.selectAppointmentVaccinationByRecordId(recordId);
    }

    /**
     * 查询预约接种记录列表
     *
     * @param appointmentVaccination 预约接种记录
     * @return 预约接种记录
     */
    @Override
    public List<AppointmentVaccination> selectAppointmentVaccinationList(AppointmentVaccination appointmentVaccination) {
        Set<String> roles = SecurityUtils.getLoginUser().getRoles();
        if (roles.contains("user")) {
            appointmentVaccination.setUserId(SecurityUtils.getUserId());
        }

        return appointmentVaccinationMapper.selectAppointmentVaccinationList(appointmentVaccination);
    }

    @Autowired
    private RedissonClient redissonClient;

    /**
     * 新增预约接种记录
     *
     * @param appointmentVaccination 预约接种记录
     * @return 结果
     */
    private static final String LOCK_PREFIX = "lock:appointment:";
    @Autowired
    private RemoteInventoryService remoteInventoryService;
    @Autowired
    private RemoteNotificationService remoteNotificationService;

    @Override
    public int insertAppointmentVaccination(AppointmentVaccination appointmentVaccination) {
        //通过疫苗id 加分布式锁
        Long vaccineId = appointmentVaccination.getVaccineId();
        Vaccine vaccine = new Vaccine();
        vaccine.setVaccineId(vaccineId);
        vaccine.setQuantity(-1L);
        //减库存
        remoteInventoryService.updateVaccineStock(vaccine);
        appointmentVaccination.setCreateTime(DateUtils.getNowDate());
        appointmentVaccination.setUserId(SecurityUtils.getUserId());
        int i = appointmentVaccinationMapper.insertAppointmentVaccination(appointmentVaccination);
        //发邮件
        if (StringUtils.isNotBlank(appointmentVaccination.getUserEmail())) {
            EmailVO emailVO = new EmailVO();
            emailVO.setToEmail(appointmentVaccination.getUserEmail());
            emailVO.setSubject("您的疫苗接种已预约");
            Date appointmentTime = appointmentVaccination.getAppointmentTime();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String strTime = sdf.format(appointmentTime);
            String location = appointmentVaccination.getLocation();

            emailVO.setContent("尊敬的用户，\n" +
                    "感谢您的预约！您的疫苗接种已成功安排，请在预约时间内前往接种。\n" +
                    "请携带身份证件，并确保准时到达。\n" +
                    "期待您的到来，祝您健康！\n" +
                    "预约时间:"+strTime+"\n" +
                    "预约地点:"+location);
            remoteNotificationService.send(emailVO);
        }
        return i;

    }


    /**
     * 修改预约接种记录
     *
     * @param appointmentVaccination 预约接种记录
     * @return 结果
     */
    @Override
    public int updateAppointmentVaccination(AppointmentVaccination appointmentVaccination) {
        return appointmentVaccinationMapper.updateAppointmentVaccination(appointmentVaccination);
    }

    /**
     * 批量删除预约接种记录
     *
     * @param recordIds 需要删除的预约接种记录主键
     * @return 结果
     */
    @Override
    public int deleteAppointmentVaccinationByRecordIds(Long[] recordIds) {
        return appointmentVaccinationMapper.deleteAppointmentVaccinationByRecordIds(recordIds);
    }

    /**
     * 删除预约接种记录信息
     *
     * @param recordId 预约接种记录主键
     * @return 结果
     */
    @Override
    public int deleteAppointmentVaccinationByRecordId(Long recordId) {
        return appointmentVaccinationMapper.deleteAppointmentVaccinationByRecordId(recordId);
    }
}
